import log from '../helpers/logger';
import { CacheInterface } from 'node-easywechat';

class RedisCacher extends CacheInterface {
  private redis;

  constructor(redis) {
    super()
    this.redis = redis;
    
  }

  get(id)
  {
    log.info({ info: 'RedisCacherget', id });
    return this.redis.get(id);
  }

  async has(id)
  {
    log.info({ info: 'RedisCacherhas', id });
    return (await this.redis.exists(id)) > 0;
  }

  async set(id, data = null, lifeTime = 0)
  {
    log.info({ info: 'RedisCacherset', id });
    log.info({ info: 'RedisCachersetdata', data });
    log.info({ info: 'RedisCachersetlifeTime', lifeTime });
    if (lifeTime > 0) {
      await this.redis.set(id, data, 'EX', lifeTime);
    }
    else {
      await this.redis.set(id, data);
    }
    return true;
  }

  async delete(id)
  {
    log.info({ info: 'RedisCacherdelete', id });
    await this.redis.del(id);
    return true;
  }
}

export default RedisCacher;

